<?php
	class Default_Model_Mailer
	{

		private $_params = null;
		private $_mailer = null;

	    public function __construct()
	    {

	        $siteConfig = Zend_Registry::get('siteConfig');

	        if (empty($siteConfig)) {
	        	throw new Exception('Could not find site emails config...');
	        }


			$this->_params = $siteConfig->emails;

	        if ($this->_params->transport == 'smtp') {
				$transport = new Zend_Mail_Transport_Smtp(
					$this->_params->host, array(
						'auth' => 'login',
						'username' => $this->_params->username,
						'password' => $this->_params->password,
						'ssl' => 'tls',
						'port' => $this->_params->port
					)
				);


	        } else {
				$transport = new Zend_Mail_Transport_Sendmail('-f'.$this->_params->return->email);
	        }

			Zend_Mail::setDefaultTransport($transport);
			$this->_mailer = new Zend_Mail($siteConfig->encoding);

			//$this->_mailer->setHeaderEncoding();

	    }


	    public function getMailView($siteConfig='') {

    		try {
                if (!empty($this->_params->templates)) {
                    $templatesPath = $this->_params->templates;
                } else if (!empty($siteConfig)) {
                    $templatesPath = $siteConfig->emails->templates;
                } else {
                    $siteConfig = Zend_Registry::get('siteConfig');
                    $templatesPath = $siteConfig->emails->templates;
                }


				$mailView = new Zend_View();
                $mailView->setScriptPath($templatesPath);
                $mailView->addHelperPath("My/View/Helper", "My_View_Helper");

                return $mailView;

    		} catch (Exception $e) {
    			throw $e;
    		}
	    }

	    public function getMailUserTemplates() {

    		try {
    			return array(
					'foreign-student-no-application-form-warning.phtml',
					//'foreign-student-new-user-welcome.phtml',
					//'foreign-student-nomination-information.phtml',
					//'foreign-student-no-application-form-warning.phtml',
    			);

    		} catch (Exception $e) {
    			throw $e;
    		}
	    }

	    public function send($mailData)
	    {
	    	/*
			$this->_mailer->clearSubject();
    		$this->_mailer->clearRecipients();
    		$this->_mailer->clearDefaultFrom();
    		$this->_mailer->clearDefaultReplyTo();
			*/

	        if (!is_array($mailData) || empty($mailData)) {
	            throw new Exception("email data is not array or empty");
	        }
	        $mailData = (object) $mailData;

			//if (APPLICATION_ENV != 'production') {
			if ($this->_params->delivery == 'only_admins') {
				$mailData->to = $this->_params->admin->email;
			}

			if (empty($mailData->from)) {
				$mailData->from = $this->_params->from->email;
			}


			//$translateObj = Zend_Registry::get('translate');
			$customObj = new My_Custom_Functions();
			$defaultLocaleObj = Zend_Registry::get('Zend_Locale');
			//add signature
			if ($mailData->signature_language == 'en') {
				//$signatureContent = $this->_params->signature_en;
				$signatureContent = $customObj->specialTranslations('emails_signature', 'en_US');
                $doNotReplyText = $customObj->specialTranslations('emails_do_not_reply_mail_text', 'en_US');
			} else {
				//$signatureContent = $this->_params->signature;
				$signatureContent = $customObj->specialTranslations('emails_signature', $defaultLocaleObj);
				$doNotReplyText = $customObj->specialTranslations('emails_do_not_reply_mail_text', $defaultLocaleObj);
			}



			//if ($this->_params->signature != '') {
			if (!empty($signatureContent)) {
                $signatureView = $this->getMailView();
				$signatureView->signatureText = $signatureContent;
				$signatureView->doNotReplyText = $doNotReplyText;
				$mailData->body .= $signatureView->render('signature.phtml');
			}

			if ($mailData->mailBodyType == 'text') {
				$this->_mailer->setBodyText($mailData->body);
			} else {
				$this->_mailer->setBodyHtml($mailData->body);
			}

			//TO recipients
			if (is_array($mailData->to)) {
				foreach ($mailData->to as $key=>$emailTo) {
					$this->_mailer->addTo($emailTo, $mailData->toLabel);
				}
			} else {
				$this->_mailer->addTo($mailData->to, $mailData->toLabel);
			}

			//CC recipients
            if (!empty($mailData->bcc)) {
                if (is_array($mailData->cc)) {
                    foreach ($mailData->cc as $keyCC=>$emailCC) {
                        //$this->_mailer->addCc($emailCC, $mailData->ccLabel);
                        $this->_mailer->addCc($emailCC);
                    }
                } else {
                    //$this->_mailer->addCc($mailData->cc, $mailData->ccLabel);
                    $this->_mailer->addCc($mailData->cc);
                }
            }

			//BCC recipients
            if (!empty($mailData->bcc)) {
                if (is_array($mailData->bcc)) {
                    foreach ($mailData->bcc as $keyBCC=>$emailBCC) {
                        //$this->_mailer->addBcc($emailBCC, $mailData->bccLabel);
                        $this->_mailer->addBcc($emailBCC);
                    }
                } else {
                    //$this->_mailer->addBcc($mailData->bcc, $mailData->bccLabel);
                    $this->_mailer->addBcc($mailData->bcc);
                }
			}

			//set reply to
			if (!empty($mailData->replyTo)) {
				$this->_mailer->setReplyTo($mailData->replyTo);

			} else if (!empty($this->_params->replyto->email)) {
				$this->_mailer->setReplyTo($this->_params->replyto->email);

			} else {

			}

			$this->_mailer->setSubject($mailData->subject);
			$this->_mailer->setFrom($mailData->from, $mailData->fromLabel);

            //if (CLIENT_NAME != 'demo') {//demoda kimseye mail atmasin - mailler gitmesin...
			    //print_r($this->_mailer);exit;
			    $this->_mailer->send();
            //}

//$mailData->copyToAdmin=true;
	    	//if ( !in_array(APPLICATION_ENV, array('development')) || (CLIENT_NAME == 'demo') || ($mailData->copyToAdmin===true) ) {
    		if ( ( (CLIENT_NAME == 'demo') || ($mailData->copyToAdmin===true) ) || ($this->_params->copyToAdmin == true) ) {
	    		$this->_mailer->clearRecipients();
				$this->_mailer->addTo($this->_params->admin->email, $this->_params->admin->label);

				$this->_mailer->clearSubject();
				$this->_mailer->setSubject($mailData->subject.'[ADMIN]');
				//$this->_mailer->setFrom($mailData->from, $mailData->fromLabel);
				$this->_mailer->send();

			}

			return true;

	    }


	    //use this function to send admin an alert from anywhere you want
		public function sendAlertMail($subject='', $message='') {
			$subject = empty($subject) ? 'ALERTS' : '[ALERTS] '.$subject;
			$message = empty($message) ? 'NOTIFICATION' : $message;


			$siteConfig = Zend_Registry::get('siteConfig');
			$userAuthData = array();
			$userAuthData = (object)$userAuthData;
			if (Zend_Registry::isRegistered('userAuthData')) {
				$userAuthData = Zend_Registry::get('userAuthData');
			}

			$mailer = new Default_Model_Mailer();
			$mailView = $mailer->getMailView();
		    $mailView->message 	 = $message;
		    $mailView->authdata  = $userAuthData;

			$bodyText  = $mailView->render('alert.admin.phtml');

			$this->_mailer->setSubject($subject);
			$this->_mailer->setBodyHtml($bodyText);
			$this->_mailer->addTo($siteConfig->emails->admin->email, $siteConfig->emails->admin->label);
			$this->_mailer->setReplyTo($siteConfig->emails->admin->email);
			$this->_mailer->setFrom($siteConfig->emails->from->email, $siteConfig->emails->from->label);

			$this->_mailer->send();

		}


	}